﻿using System;
using System.Linq;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;
using System.Collections.Generic;

public partial class public_html_NewSite_CMSV2_GamesList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["Search"] != null)
            {
                uiTxtSearch.Text = Server.UrlDecode(Request.QueryString["Search"]);
                uiBtnSearch_Click(uiBtnSearch, null);
            }
            else
            {
                BindToAll();
            }
        }
    }

    private void BindToAll()
    {
        int viewOption = 0;
        if (Request["ViewOption"] != null)
        {
            if (!int.TryParse(Request["ViewOption"], out viewOption))
            {
                viewOption = 0;
            }
        }

        ResetLinks(viewOption);
        if (viewOption.Equals(1))
        {
            BindToMissingScreenshots();
            return;
        }

        uiBlGames.Items.Clear();
        using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
        using (IGameService service = ServiceFactory.GameService)
        {
            Format[] formats = formatService.GetAll().ToArray();
            Game[] allGames = service.GetAll().OrderBy(t => t.Title).ToArray();
            allGames = Array.FindAll<Game>(allGames, g => g.Format > 0 && Array.FindIndex<Format>(formats, f => f.Id.Equals(g.Format)) >= 0);
            switch (viewOption)
            {
                case 2:
                    allGames = allGames.Where(t => !(System.IO.File.Exists(MapPath(ResolveUrl("~/covers/" + t.GameId.ToString() + "_" + t.Format.ToString() + ".png")))
                        || System.IO.File.Exists(MapPath(ResolveUrl("~/covers/" + t.GameId.ToString() + "_" + t.Format.ToString() + ".jpg"))))).ToArray();
                    break;
                case 3:
                    allGames = allGames.Where(t => t.ReleaseDate.Date.Equals(DateTime.MinValue.Date)).ToArray();
                    break;
                case 4:
                    allGames = allGames.Where(t => String.IsNullOrEmpty(t.Publisher) || String.IsNullOrEmpty(t.Developer)).ToArray();
                    break;
            }
            Array.Sort<Game>(allGames, (item1, item2) => item1.Title.CompareTo(item2.Title));
            Array.ForEach<Game>(allGames, t => uiBlGames.Items.Add(
                        new ListItem(
                            String.Format("{0} ({1})", t.Title, Array.Find<Format>(formats, fo => fo.Id.Equals(t.Format)).Name)
                            , String.Format("GameManager.aspx?gameId={0}", t.GameId.ToString()))));
        }

    }

    private void BindToMissingScreenshots()
    {
        uiBlGames.Items.Clear();
        using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
        using (IGameService service = ServiceFactory.GameService)
        {
            Format[] formats = formatService.GetAll().ToArray();
            Game[] allGames = service.FindGamesWithMissingScreenshots().OrderBy(t => t.Title).ToArray();
            if (allGames.Length > 0)
            {
                allGames = Array.FindAll<Game>(allGames, g => g.Format > 0 && Array.FindIndex<Format>(formats, f => f.Id.Equals(g.Format)) >= 0);
                Array.Sort<Game>(allGames, (item1, item2) => item1.Title.CompareTo(item2.Title));
                Array.ForEach<Game>(allGames, t => uiBlGames.Items.Add(
                            new ListItem(
                                String.Format("{0} ({1})", t.Title, Array.Find<Format>(formats, fo => fo.Id.Equals(t.Format)).Name)
                                , String.Format("GameManager.aspx?gameId={0}", t.GameId.ToString()))));
            }
            else
            {
                uiBlGames.Items.Add(new ListItem("Ingen spil fundet", "", false));
            }
        }
    }

    protected void uiBtnSearch_Click(object sender, EventArgs e)
    {
        ResetLinks(-1);
        uiBlGames.Items.Clear();
        using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
        using (IGameService service = ServiceFactory.GameService)
        {
            Format[] formats = formatService.GetAll().ToArray();
            Game[] allGames = service.FindGamesByTitle(uiTxtSearch.Text).OrderBy(t => t.Title).ToArray();
            if (allGames.Length > 0)
            {
                allGames = Array.FindAll<Game>(allGames, g => g.Format>0 && Array.FindIndex<Format>(formats, f => f.Id.Equals(g.Format)) >= 0);
                Array.Sort<Game>(allGames, (item1, item2) => item1.Title.CompareTo(item2.Title));
                Array.ForEach<Game>(allGames, t => uiBlGames.Items.Add(
                            new ListItem(
                                String.Format("{0} ({1})", t.Title, Array.Find<Format>(formats, fo => fo.Id.Equals(t.Format)).Name)
                                , String.Format("GameManager.aspx?gameId={0}", t.GameId.ToString()))));
            }
            else
            {
                uiBlGames.Items.Add(new ListItem("Ingen spil fundet", "", false));
            }
        }
    }

    private void ResetLinks(int viewOption)
    {
        uiHlGamesAll.Enabled = true;
        uiHlGamesNoCover.Enabled = true;
        uiHlGamesNoPublisherDeveloper.Enabled = true;
        uiHlGamesNoReleaseDate.Enabled = true;
        uiHlGamesNoScreenshot.Enabled = true;

        switch (viewOption)
        {
            case 0:
                uiHlGamesAll.Enabled = false;
                break;
            case 1:
                uiHlGamesNoScreenshot.Enabled = false;
                break;
            case 2:
                uiHlGamesNoCover.Enabled = false;
                break;
            case 3:
                uiHlGamesNoReleaseDate.Enabled = false;
                break;
            case 4:
                uiHlGamesNoPublisherDeveloper.Enabled = false;
                break;
        }
    }
}
